﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CivilRegistry.ControlledModel;
using CivilRegistry.Common;

namespace CivilRegistry
{
    public partial class BirthReport : System.Web.UI.Page
    {
        Hospital loggedInUser = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (SessionHandler.CurrentSession.User == null)
            {
                Response.Redirect("~/Login.aspx?&redirect=BirthCertificate.aspx");
            }

            SiteMaster masterPage = (SiteMaster)Page.Master;
            masterPage.SelectedMenu = "BirthReport";

            this.loggedInUser = SessionHandler.CurrentSession.User as Hospital;

            if (this.loggedInUser == null)
            {
                Response.Redirect("~/Default.aspx");
            }

            if (!Page.IsPostBack)
            {
                if (this.loggedInUser != null)
                {
                    panelNewBirthReport.Visible = false;
                    panelAvailableBirthReports.Visible = true;
                }
            }

            lblStatus.Text = "";
        }

        protected void gvBirthReport_Sorting(object sender, GridViewSortEventArgs e)
        {
            if (SessionHandler.CurrentSession.SortDirection == SortDirection.Descending)
            {
                SessionHandler.CurrentSession.SortDirection = SortDirection.Ascending;
            }
            else
            {
                SessionHandler.CurrentSession.SortDirection = SortDirection.Descending;
            }

            this.FillBirthCertificateGrid(e.SortExpression, SessionHandler.CurrentSession.SortDirection);
        }

        private void FillBirthCertificateGrid(string columnName, SortDirection sortDirection)
        {
            int pageIndex = gvBirthReport.PageIndex;

            gvBirthReport.DataSource = (from birthReport in this.loggedInUser.GetBirthReports()
                                        select new
                                            {
                                                BirthReportId = birthReport.BirthReportId,
                                                ParentId = birthReport.AssociatedParent.UserId,
                                                ParentName = birthReport.AssociatedParent.FirstName,
                                                DOB = birthReport.DateOfBirthOftheChild,
                                                VerificationCode = birthReport.VerificationCode
                                            }).OrderBy(columnName, sortDirection);

            gvBirthReport.DataBind();
            gvBirthReport.PageIndex = pageIndex;
        }

        protected void gvBirthReport_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvBirthReport.PageIndex = e.NewPageIndex;
            this.FillBirthCertificateGrid();
        }

        private void FillBirthCertificateGrid()
        {
            this.FillBirthCertificateGrid("BirthReportId", SortDirection.Ascending);
        }

        protected void gvBirthReport_RowEditing(object sender, GridViewEditEventArgs e)
        {
            e.Cancel = true;
        }

        protected void btnViewAll_Click(object sender, EventArgs e)
        {
            gvBirthReport.Visible = true;
            pnlTools.Visible = false;
            panelNewBirthReport.Visible = false;

            this.FillBirthCertificateGrid();
        }

        protected void btnNewBirthReport_Click(object sender, EventArgs e)
        {
            pnlTools.Visible = false;
            gvBirthReport.Visible = false;
            panelNewBirthReport.Visible = true;
            pnlUserGadget.Visible = false;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            var person = Person.FindById(Convert.ToInt32(txtParentId.Text));
            if (person != null)
            {
                var birthReport = new ControlledModel.BirthReport();

                birthReport.CivilHospital = this.loggedInUser;
                birthReport.AssociatedParent = person;
                birthReport.GenderOftheChild = radioGender.SelectedValue;
                birthReport.DateOfBirthOftheChild = Convert.ToDateTime(txtDateOfBirth.Text);
                birthReport.VerificationCode = this.GetVerificationCode(6); // a 6 charecter code.

                this.loggedInUser.AddBirthReport(birthReport);

                lblStatus.Text = "Application sent successfully.";

                EmailHelper.SendVerificationCode(person, birthReport.VerificationCode, ref lblStatus);

                pnlTools.Visible = true;
                panelNewBirthReport.Visible = false;
                pnlUserGadget.Visible = true;
            }
            else
            {
                lblStatus.Text = "Invalid person Id.";
            }
        }

        private string GetVerificationCode(int length)
        {
            char[] chars = "$%#@!*abcdefghijklmnopqrstuvwxyz1234567890?;ABCDEFGHIJKLMNOPQRSTUVWXYZ^&".ToCharArray();
            string password = string.Empty;
            Random random = new Random();

            for (int i = 0; i < length; i++)
            {
                int x = random.Next(1, chars.Length);
                //Don't Allow Repetation of Characters
                if (!password.Contains(chars.GetValue(x).ToString()))
                    password += chars.GetValue(x);
                else
                    i--;
            }
            return password;
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            pnlTools.Visible = true;
            panelNewBirthReport.Visible = false;
            pnlUserGadget.Visible = true;
        }
    }
}